package public

import (
	"server/game/cache"
	"server/game/model"
	"server/share/config"

	"github.com/hwcer/cosgo/registry"
	"github.com/hwcer/updater/dataset"
	"github.com/hwcer/yyds"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
)

/**
 * @name team
 * 队伍相关
 */

func init() {
	Register(&team{})
}

type team struct {
}

func (this *team) Caller(node *registry.Node, handle *context.Context) interface{} {
	f := node.Method().(func(*team, *context.Context) interface{})
	return f(this, handle)
}

// Get 获取阵容列表
func (this *team) Get(c *context.Context) any {
	coll := c.Player.Collection(config.ITypeTeams)
	var result []*model.Teams
	coll.Range(func(id string, doc *dataset.Document) bool {
		i := doc.Any().(*model.Teams)
		result = append(result, i)
		return true
	})
	return result
}

/**
 * @name setTeamName
 * @param int id 队伍ID
 * @param string name 队伍名字
 * 修改名字
 */

func (this *team) SetTeamName(c *context.Context) any {
	id := c.GetInt32("id")
	name := c.GetString("name")
	//if name == "" {
	//	return errors.ErrArgEmpty
	//}
	if i := config.Data.Team[id]; i == nil {
		return errors.ErrConfigEmpty
	}
	coll := c.Player.Collection(config.ITypeTeams)
	coll.Set(id, "name", name)
	return nil

}

/**
 * @name updateTeam
 * @param int id 队伍ID
 * @param string name 队伍名字
 * @param json team 阵容数据
 * 修改阵容
 */

func (this *team) UpdateTeam(c *context.Context) any {
	args := struct {
		Id   int32
		Name string
		Team map[string]string
	}{
		Team: make(map[string]string),
	}
	if err := c.Bind(&args); err != nil {
		return err
	}

	tc := config.Data.Team[args.Id]
	if tc == nil {
		return errors.ErrConfigEmpty
	}
	tg := config.Data.TeamGroup[tc.Group]
	if tg == nil {
		return errors.ErrConfigEmpty
	}
	if l := int32(len(args.Team)); l > tg.UnitNum {
		return c.Error("team should not exceed unit num")
	}

	items := cache.GetItems(c.Player.Updater)
	unit := map[int32]int32{}
	camp := map[int32]int32{}  //阵营统计 ????
	limit := map[int32]int32{} //职业统计
	baby := int32(0)
	for _, v := range args.Team {
		if v == "" {
			continue
		}
		d := items.Get(v)
		if d == nil {
			return c.Errorf(0, "unit %s not exist", v)
		}
		iid := d.IID
		//判断是幼体
		if yyds.Config.Is(iid, config.ITypeUnitBase) {
			baby += 1
			if ub := config.Data.UnitBase[iid]; ub == nil {
				return c.Errorf(0, "unitBase %d not exist", iid)
			} else {
				iid = ub.Id
			}
		}
		//禁止重复
		if tg.Repeat == 0 && unit[iid] > 0 {
			return c.Error("repeat count not equal team count")
		}
		unit[iid] += 1
		uc := config.Data.Unit[iid]
		if uc == nil {
			return c.Errorf(0, "unit %s not exist", d.IID)
		}
		//tag
		for _, tag := range uc.Tag {
			if tag > 0 {
				limit[tag] += 1
			}
		}
		//阵营
		camp[uc.Race] += 1
	}

	//禁止幼体
	if tg.Baby == 0 && baby > 0 {
		return c.Error("unit baby disabled")
	}
	//禁用阵营
	for _, k := range tg.Camp {
		if k > 0 && camp[k] > 0 {
			return c.Errorf(0, "camp disabled :%d", k)
		}
	}
	//limit
	for _, k := range tg.Limit {
		if k > 0 && limit[k] > 0 {
			return c.Errorf(0, "职业禁用 :%d", k)
		}
	}

	coll := c.Player.Collection(config.ITypeTeams)
	up := dataset.Update{}
	if args.Name != "" {
		up["name"] = args.Name
	}

	up["value"] = args.Team
	coll.Set(args.Id, up)

	return nil
}
